package com.vnaloha.common.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.vnaloha.common.model.Gallery;
import com.vnaloha.common.service.GalleryService;

@Controller
@RequestMapping(value = "gallery")
public class GalleryController {
	
	/**
	 * The log
	 */
	private Logger logger = LoggerFactory.getLogger(GalleryController.class);
	
	@Autowired
	private GalleryService galleryService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String index(ModelMap modelMap) {
		try {
			modelMap.addAttribute("galleries", galleryService.list());
		} catch (Exception e) {
			logger.error("Error when get index of document " + e, e);
		}
		return "manageGallery";
	}
	
	@RequestMapping(value = "/addForm.htm")
	public String addForm(ModelMap modelMap) {
		
		modelMap.addAttribute("gallery", new Gallery());
		return "addGallery";
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(@ModelAttribute("gallery") @Valid Gallery gallery, BindingResult result, ModelMap modelMap, 
					  @RequestParam("file") MultipartFile file, HttpServletRequest request) {
		
		
		if(result.hasErrors()) {
			return "addGallery";
		} else {
			galleryService.save(request, file, gallery);
			return "redirect:/gallery";	
		}
	}
	
	@RequestMapping("/edit/{userId}")
	public String edit(@PathVariable("userId") Integer id, ModelMap modelMap) {
		
		Gallery gallery = galleryService.get(id);
		if(null != gallery) {
			modelMap.addAttribute("gallery", gallery);
			return "editGallery";
		} 
		return "redirect:/gallery";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(@ModelAttribute("gallery") @Valid Gallery gallery, BindingResult result, ModelMap modelMap,
					   @RequestParam("file") MultipartFile file, HttpServletRequest request) {
		if(result.hasErrors()) {
			return "editGallery";
		} else {
			return "editGallery";
		}
	}
	
	@RequestMapping("/delete/{id}")
	public String deleteGallery(@PathVariable("id") Integer id, HttpServletRequest request) {
		
		galleryService.delete(id, request);
		return "redirect:/gallery";
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(@RequestParam("listId") String listId, HttpServletRequest request) {
		
		Map<String, Object> map = new HashMap<>();
		String message = null;
		boolean result = galleryService.deleteByListId(listId, request);
		if(result) {
			message = "Deleted successfully";
		} else {
			message = "Server had some errors! Please try later.";
		}
		// put value response to client
		map.put("success", result);
		map.put("message", message);
		
		return map;
	}
	
}
